# encoding utf-8

import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import codecs
import os
from basic.utils.path import Path
from basic.utils.readConfig.GetConfigValue import GetValue

base = os.getcwd()
path = (base + '/config.txt')

def getConfig():
    configText=GetValue.read("emailname","name")
    return configText

def sendEmail(title,body,isSend):
    sender = 'ybbtest@163.com'
    receiver = getConfig()+',xiao2739@dingtalk.com'
    smtpserver = 'smtp.163.com'
    username = 'ybbtest'
    password = 'ybb123456'
    mail_title = title

    # 创建一个带附件的实例
    message = MIMEMultipart()
    message['From'] = sender
    message['To'] = receiver
    message['Subject'] = Header(mail_title, 'utf-8')

    # 邮件正文内容
    message.attach(MIMEText(body, 'html', 'utf-8'))

    path = Path.getProjectPath()
    # 构造附件1（附件为TXT格式的文本）
    # att1 = MIMEText(open('test.html', 'rb').read(), 'base64', 'utf-8')
    # att1["Content-Type"] = 'application/octet-stream'
    # att1["Content-Disposition"] = 'attachment; filename="test.html"'
    # message.attach(att1)

    #构造附件3（附件为HTML格式的网页）
    # att3 = MIMEText(open(path+'/res/report/'+'test.html', 'rb').read(), 'base64', 'utf-8')
    # att3["Content-Type"] = 'application/octet-stream'
    # att3["Content-Disposition"] = 'attachment; filename="report_test.html"'
    # message.attach(att3)

    try:
        smtpObj = smtplib.SMTP(smtpserver,25)  # 注意：如果遇到发送失败的情况（提示远程主机拒接连接），这里要使用SMTP_SSL方法
        smtpObj.connect(smtpserver,25)
        smtpObj.login(username, password)
        if isSend==0:
            smtpObj.sendmail(sender, receiver.split(','), message.as_string())
            print("邮件发送成功！！！")
        else:
            print("不发送邮件")
    except Exception as e:
        print(e)
    smtpObj.quit()

# sendEmail('','','')